//
//  LoginViewModel.m
//  CBT
//
//  Created by 四点钟 on 2018/11/15.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "LoginViewModel.h"
#import "LoginManager.h"
#import "CBTLocalUserManager.h"

@implementation LoginViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)setUp{
    
//    //RACObserve可以把KVO转化为信号
//    _btnEnableSignal =  [RACSignal combineLatest:@[RACObserve(self,account),RACObserve(self, password)] reduce:^id _Nullable(NSString * account,NSString * password){
//        return @(account.length && (password.length > 5));
//    }];
    
    
//    self.listSuccessObject = [RACSubject new];
//    
//    [RACObserve(self, phoneStr) subscribeNext:^(NSString *value) {
//        BOOL isNum = [NSString deptNumInputShouldNumber:value];
//        [self.listSuccessObject sendNext:@(isNum)];
//    }];
    
    _loginCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        NSString *password = input[@"password"];
        if([[input allKeys] containsObject:@"email"]){
            if ([NSString isBlankString:input[@"email"]]) {
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入邮箱",nil)];
                return [RACSignal error:nil];
            }else if (![NSString validateEmail:input[@"email"]]){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入正确邮箱",nil)];
                return [RACSignal error:nil];
            }else if ([NSString isBlankString:password]){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入密码",nil)];
                return [RACSignal error:nil];
            }
        }else if([[input allKeys] containsObject:@"phone"]){
            if ([NSString isBlankString:input[@"phone"]]) {
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入手机号",nil)];
                return [RACSignal error:nil];
            }else if (![NSString isInputRightPhoneNum:input[@"phone"]]){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入正确手机号",nil)];
                return [RACSignal error:nil];
            }else if ([NSString isBlankString:password]){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入密码",nil)];
                return [RACSignal error:nil];
            }else if (password.length < 8 || password.length > 20){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入8-20位的数字字母相结合的密码",nil)];
                return [RACSignal error:nil];
            }
        }
        
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            NSString *area = [[NSUserDefaults standardUserDefaults] objectForKey:@"area"];

            [LoginManager phoneaAndEmailLoginWithID:[[input allKeys] containsObject:@"email"] ? input[@"email"] : input[@"phone"] isPhone:[[input allKeys] containsObject:@"phone"]  pwd:input[@"password"] area:area block:^(id data, HttpRequestResult result, NSString *msg) {
                if (result == kHttpRequestResultNormal) {
                    [[NSUserDefaults standardUserDefaults]setObject:[data[@"data"]objectForKey:@"token"] forKey:HEFANTV_TOKEN];
                    [[NSUserDefaults standardUserDefaults]setObject:input[@"password"] forKey:HEFANTV_PassWord];
                    [[CBTLocalUserManager sharedInstance] saveALLUserInfoDic:data];
                    
                    if (![NSString isBlankString:[data[@"data"]objectForKey:@"picUrl"]]) {
                        dispatch_async(dispatch_get_global_queue(0, 0), ^{
                            
                            [CBTImageTools saveUserImageWithImage:[UIImage sd_animatedGIFWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",kBaseURL_PICSTATIC,[data[@"data"]objectForKey:@"picUrl"]]]]] imageName:[NSString stringWithFormat:@"%@",[data[@"data"]objectForKey:@"picUrl"]] userID:[data[@"data"]objectForKey:@"userId"]];
                        });
                    }
                    
                    [subscriber sendNext:data];
                    [subscriber sendCompleted];
                }else{
                    [HFStatusView showFailureMessage:msg];
                    [subscriber sendError:nil];   
                    [subscriber sendCompleted];
                }
            }];
            
            return [RACDisposable disposableWithBlock:^{
                NSLog(@"%@登录页面操作结束了",[[input allKeys] containsObject:@"email"] ? @"邮箱" : @"手机号");
            }];
        }];
    }];
    
}

@end
